﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml.Serialization;
using Microsoft.Xna.Framework.Input;
using Kristall.MyGame.Scripts;
using System.ComponentModel;
using Kristall.Engine;
using Kristall.Engine.Scripts;

namespace Kristall
{
    public enum CrystalType
    {
        Red,
        Green,
        Blue,
        None
    }

    [Serializable]
    public class Crystal : Animatable
    {
        public CrystalType Type { get; set; }

        [XmlIgnore]
        private Light _light = new Light();

        [XmlIgnore]
        Player _player;

        public float DrainSpeed
        {
            get
            {
                return Kristall.Options.drainspeed;
            }
        }

        private float _energy;

        [XmlIgnore, Browsable(false)]
        public float Energy
        {
            get
            {
                return _energy;
            }
            set
            {
                _energy = MathHelper.Clamp(value, 0, 1.0f);

                if (_energy <= 0)
                {
                    Drained = true;

                    if (OnDrained != null)
                        OnDrained.Trigger();
                }
                else if (_energy >= 1.0f)
                {
                    if (OnCharged != null)
                        OnCharged.Trigger();
                }

                _light.Color = GetColor() * _energy;
            }
        }


        [Browsable(false), XmlIgnore]
        public override List<Script> Scripts
        {
            get;
            set;
        }


        public TriggerScript OnDrained { get; set; }
        public TriggerScript OnCharged { get; set; }

        public bool Drained { get; set; }
        bool canDrain = true;

        public Crystal() : base()
        {
            this.CheckWorldCollision = false;
            this.CheckPlayerCollision = false;

            _energy = 0;

            OnDrained = new TriggerScript();
            OnCharged = new TriggerScript();

            OnDrained.Id = "OnDrained";
            OnCharged.Id = "OnCharged";

            Drained = false;
        }

        public Color GetColor()
        {
            switch (Type)
            {
                case CrystalType.Red:
                    return Color.Red;
                case CrystalType.Green:
                    return Color.Green;
                case CrystalType.Blue:
                    return Color.Blue;
            }

            return Color.White;
        }

        public override void LoadContent(Level level)
        {
            _light.Color = GetColor() * _energy;

            Animations.Clear();
            Animations.Add(new AnimatableSprite(@"Sprites\Crystal.xnb"));
            base.LoadContent(level);
            CurrentAnimationKey = @"Sprites\Crystal.xnb";
            CurrentAnimation.Tint = new Color(Vector4.One * 0.5f + GetColor().ToVector4());

            CurrentAnimation.FrameWidth = level.SpriteTextures[CurrentAnimationKey].Width;
            CurrentAnimation.FrameHeight = level.SpriteTextures[CurrentAnimationKey].Height;

            SetSize(CurrentAnimation.FrameWidth, CurrentAnimation.FrameHeight);

            _light.SetSize(this.HalfSize * 2f);
            _light.Position = this.Position;
            _light.Texture = "Crystal";
            level.DynamicLights.Add(_light);

            OnDrained.Load(level, this);
            OnCharged.Load(level, this);

            _player = level.Player;
        }


        public override void RemoveFromLevel()
        {
            Level.Crystals.Remove(this);
            Level.DynamicLights.Remove(_light);
        }

        public override void AddToLevel()
        {
            if (this.Id.Equals("Id_not_set"))
                this.Id = "Crystal" + Level.Crystals.Count;

            Level.Crystals.Add(this);
            Level.DynamicLights.Add(_light);
        }

        public override void Update(float dt)
        {
            base.Update(dt);

            if (Kristall.ScreenManager.Input.LastKeyboardStates.IsKeyUp(Keys.C))
            { 
                canDrain = true;
            }

            if (_player.Bounds.Intersects(this.Bounds) && Kristall.ScreenManager.Input.CurrentKeyboardStates.IsKeyDown(Keys.C))
            {
                if (canDrain)
                {
                    _player.Crystal = this;
                    if (this.Drained)
                    {
                        if (_player.EnergyType == this.Type)
                        {
                            Energy += dt * DrainSpeed;

                            if (Energy >= 1)
                            {
                                Drained = canDrain = false;
                                _player.EnergyType = CrystalType.None;
                            }
                        }
                    }
                    else
                    {
                        if (_player.EnergyType == CrystalType.None)
                        {
                            Energy -= dt * DrainSpeed;

                            if (Drained)
                            {
                                _player.EnergyType = this.Type;
                                canDrain = false;
                            }
                        }
                    }
                }
                else
                {

                }
            }
            else
            {
                if (!Drained)
                {
                    if (_energy < 1.0f)
                        Energy += dt * DrainSpeed;
                }
                else
                {
                    Energy -= dt * DrainSpeed;
                }
            }
        }

    }
}
